package com.smyhvae.wifidemo;

import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private static final String TAG = "MainActivity";
    private Button btn_open;
    private Button btn_close;
    private Button btn_getWifiList;
    private Button btn_getWifiInfo;
    private Button btn_getWifiState;
    private TextView tv_wifiInfo;
    private ListView lv_getWifiList;
    private WifiManager wifiManager;
    private List<ScanResult> mWifiList;
    private List<String> wifiList;
    private WifiInfo wifiInfo;
    private final int INFO = 1;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg){
            super.handleMessage(msg);
            switch (msg.what){
                case INFO:
                    String SSID = wifiInfo.getSSID();
                    String bssid = wifiInfo.getBSSID();
                    String macAddress = wifiInfo.getMacAddress();
                    int ipAddress = wifiInfo.getIpAddress();
                    int netWorkId = wifiInfo.getNetworkId();
                    int speed = wifiInfo.getLinkSpeed();
                    int rssi = wifiInfo.getRssi();
                    tv_wifiInfo.setText("目前连入的WiFi是：" + SSID + "\n接入点的SSID为：" + bssid + "\nMAC地址：" + macAddress +
                            "\nIP地址：" + ipAddress + "\n连接的ID：" + netWorkId + "\n速度：" + speed + "\n信号强度:" + rssi);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        findView();
        setListener();
    }

    private void setListener(){
        btn_open.setOnClickListener(this);
        btn_close.setOnClickListener(this);
        btn_getWifiList.setOnClickListener(this);
        btn_getWifiInfo.setOnClickListener(this);
        btn_getWifiState.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_open:
                Log.d(TAG, "onClick: " + "btn_open");
                if (!wifiManager.isWifiEnabled()){
                    wifiManager.setWifiEnabled(true);
                    Toast.makeText(this, "wifi已经开启", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_close:
                Log.d(TAG, "onClick: " + "btn_close");
                if (wifiManager.isWifiEnabled()){
                    wifiManager.setWifiEnabled(false);
                    Toast.makeText(this, "wifi已经关闭", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_getwifilist:
                Log.d(TAG, "onClick: " + "btn_getwifilist");
                //先判断WiFi是否已经开启
                if (wifiManager.isWifiEnabled()){
                    wifiManager.startScan();
                    mWifiList = wifiManager.getScanResults();
                    Log.d(TAG, "mWifiList: " + mWifiList.size());
                    wifiList.clear();
                    for (int i = 0; i < mWifiList.size(); i++){
                        wifiList.add(mWifiList.get(i).SSID);
                    }
                    setAdapter();
                } else {
                    Toast.makeText(this, "WiFi没有开启，无法扫描", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_getwifiinfo:
                Log.d(TAG, "onClick: " + "getwifiinfo");
                Message msg = Message.obtain();
                msg.what = INFO;
                mHandler.sendMessage(msg);
                break;
            case R.id.btn_getwifistate:
                Log.d(TAG, "onClick: " + "btn_getwifistate");
                int wifiState = wifiManager.getWifiState();
                Toast.makeText(this, "WiFi状态为：" + wifiState, Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }

    private void findView(){
        btn_open = (Button) findViewById(R.id.btn_open);
        btn_close = (Button) findViewById(R.id.btn_close);
        btn_getWifiList = (Button) findViewById(R.id.btn_getwifilist);
        btn_getWifiInfo = (Button) findViewById(R.id.btn_getwifiinfo);
        btn_getWifiState = (Button) findViewById(R.id.btn_getwifistate);
        tv_wifiInfo = (TextView) findViewById(R.id.tv_getwifiinfo);
        lv_getWifiList = (ListView) findViewById(R.id.lv_wifilist);
    }

    protected void setAdapter(){
        ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (MainActivity.this, android.R.layout.simple_list_item_1, wifiList);
        adapter.notifyDataSetChanged();
        lv_getWifiList.setAdapter(adapter);
    }

    private void init(){
        wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
        wifiInfo = wifiManager.getConnectionInfo();
        mWifiList = new ArrayList<ScanResult>();
        wifiList = new ArrayList<String>();
    }
}
